/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.util.jpa;

import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import kiff.util.Loader;
import kiff.util.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

/**
 * Entity Manager Factory Provider.
 * @author Adam
 * @version $Id: EntityManagerFactoryProvider.java 62 2008-10-31 04:51:50Z a.ruggles $
 * 
 * Created on Oct 6, 2008 at 12:19:47 PM 
 */
public class EntityManagerFactoryProvider implements Provider<EntityManagerFactory> {
	/**
	 * Default Persistence Unit Name.
	 */
	public static final String DEFAULT_PERSISTENCE_UNIT = "DefaultPU";

	/**
	 * The Entity Manager Factory.
	 */
	private final EntityManagerFactory emFactory;

    /**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private final Logger logger = LoggerFactory.getLogger(EntityManagerFactoryProvider.class);

	/**
	 * Constructs an Entity Manager Factory Provider.
	 * @param config The application Loader.
	 */
	@Inject
	public EntityManagerFactoryProvider(final Loader config) {
		logger.debug("Creating the entity manager factory");
		String unitName = config.getSetting(String.class, "config.jpa.persistenceUnit");
		if (unitName == null || unitName.trim().length() == 0) {
			unitName = DEFAULT_PERSISTENCE_UNIT;
		}
		emFactory = Persistence.createEntityManagerFactory(unitName, config.getAsProperties());
	}

	/**
	 * Constructs an Entity Manager Factory Provider.
	 * @param persistenceUnit The name of the persistence unit.
	 * @param properties Additional properties to use when creating the factory.
	 */
	public EntityManagerFactoryProvider(final String persistenceUnit, final Map<String, Object> properties) {
		String unitName = persistenceUnit;
		if (persistenceUnit == null || persistenceUnit.trim().length() == 0) {
			unitName = DEFAULT_PERSISTENCE_UNIT;
		}
		logger.debug("Creating the entity manager factory");
		if (properties != null) {
			emFactory = Persistence.createEntityManagerFactory(unitName, properties);
		} else {
			emFactory = Persistence.createEntityManagerFactory(unitName);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#finalize()
	 */
	@Override
	protected void finalize() throws Throwable {
		if (!emFactory.isOpen()) {
			emFactory.close();
		}
	}

	/**
	 * Returns an instance of the Entity Manager Factory.
	 * {@inheritDoc}
	 * @see kiff.util.Provider#get()
	 */
	public EntityManagerFactory get() {
		return emFactory;
	}
}
